//
//  MusicViewController.m
//  享动
//
//  Created by lanou on 16/4/7.
//  Copyright © 2016年 HHUI. All rights reserved.
//

#import "MusicViewController.h"
#import "musicrunCell.h"
#import "RequestManager.h"

#import "musicRunModel.h"
#import <UIImageView+WebCache.h>
@interface MusicViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,UIScrollViewDelegate>


@property(nonatomic,strong)NSArray * musicUrlArray;

@property(nonatomic,strong)NSMutableArray * modelArray1;
@property(nonatomic,strong)NSMutableArray * modelArray2;
@property(nonatomic,strong)NSMutableArray * modelArray3;
@property(nonatomic,strong)NSMutableArray * modelArray4;
@property(nonatomic,strong)NSMutableArray * modelArray5;
@property(nonatomic,strong)NSMutableArray * modelArray6;
@property(nonatomic,strong)NSMutableArray * modelArray7;
@property (weak, nonatomic) IBOutlet UIScrollView *scrolllView;
@property (weak, nonatomic) IBOutlet UICollectionView *conllectionView;

@property (weak, nonatomic) IBOutlet UIButton *musiclistButton;

@property (weak, nonatomic) IBOutlet UIButton *mymusicButton;
@property(nonatomic,strong)NSMutableArray * tempArray;

@property(nonatomic,strong)NSMutableArray * modelArray;

@end

@implementation MusicViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    _musicUrlArray = @[@"http://appt.igeekery.com//mix/scene-page.json?_ch=appStore&_k=93b67e0dfaac41078bc7666729f8e993&_lan=ch&_nw=WiFi&_product=fitmix&_sdk=iPhone%20OS8.2&_v=3&index=0&scene=3&uid=301026",@"http://appt.igeekery.com//mix/scene-page.json?_ch=appStore&_k=f98cebca56614844b86bfccd6a725f17&_lan=ch&_nw=WiFi&_product=fitmix&_sdk=iPhone%20OS8.2&_v=3&index=0&scene=2&uid=301026",@"http://appt.igeekery.com//mix/scene-page.json?_ch=appStore&_k=b9d632d4710a49b6830ea250bddff31d&_lan=ch&_nw=WiFi&_product=fitmix&_sdk=iPhone%20OS8.2&_v=3&index=0&scene=1&uid=301026",@"http://appt.igeekery.com//mix/scene-page.json?_ch=appStore&_k=23afcd25481f4710bb760935bf818599&_lan=ch&_nw=WiFi&_product=fitmix&_sdk=iPhone%20OS8.2&_v=3&index=0&scene=4&uid=301026",@"http://appt.igeekery.com//mix/scene-page.json?_ch=appStore&_k=df6f697add4c44f6aa0d328b7726d93d&_lan=ch&_nw=WiFi&_product=fitmix&_sdk=iPhone%20OS8.2&_v=3&index=0&scene=5&uid=301026",@"http://appt.igeekery.com//mix/scene-page.json?_ch=appStore&_k=a87cc5e228db4a82bdfffedbcd600b86&_lan=ch&_nw=WiFi&_product=fitmix&_sdk=iPhone%20OS8.2&_v=3&index=0&scene=6&uid=301026",@"http://appt.igeekery.com//mix/scene-page.json?_ch=appStore&_k=d37e9a14a5c5432da2fd4f3806b2c9a7&_lan=ch&_nw=WiFi&_product=fitmix&_sdk=iPhone%20OS8.2&_v=3&index=0&scene=7&uid=301026"];
    
    
    
    _modelArray  = [NSMutableArray array];
    
    for (int i = 0; i < _musicUrlArray.count; i++) {
        [RequestManager requestWithUrl:_musicUrlArray[i] requestType:RequestTypePost parDic:nil finish:^(NSData *data) {
            NSDictionary * allDic =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            NSMutableArray * resultArray  = allDic[@"page"][@"result"];
            
            
            NSDictionary * one = resultArray[0];
            musicRunModel * model = [[musicRunModel alloc]init];
            [model setValuesForKeysWithDictionary:one];
            
            [_modelArray addObject:model];
            
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.conllectionView reloadData];
            });
            
        } error:^(NSError *error) {
            NSLog(@"错误");
        }];
    }
    
    
    
    
    
    
    
    
    
    
    
    self.scrolllView.delegate = self;
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationItem.title = @"享听";
    
    
    [self.musiclistButton addTarget:self action:@selector(musiclist) forControlEvents:UIControlEventTouchUpInside];
    self.musiclistButton.backgroundColor =[UIColor colorWithRed:136/255.0 green:205/255.0 blue:85/255.0 alpha:1];
    [self.mymusicButton addTarget:self action:@selector(mymusic) forControlEvents:UIControlEventTouchUpInside];
    [self.musiclistButton.layer setCornerRadius:5];
    [self.mymusicButton.layer setCornerRadius:5];
    
    
    
    
    self.conllectionView.delegate = self;
    self.conllectionView.dataSource = self;
    self.conllectionView.backgroundColor = [UIColor whiteColor];
    [self.conllectionView registerClass:[musicrunCell class] forCellWithReuseIdentifier:@"cell"];
    
    
    
    

    
    
    
    
    
    // Do any additional setup after loading the view.
}

#pragma mark --- 返回每个分区的行数 ---
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _modelArray.count ;
    
}


#pragma mark --- 返回cell ---
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    musicrunCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
    musicRunModel * model = _modelArray[indexPath.row];
    
    [cell cellforModel:model];
    
    cell.backgroundColor = [UIColor colorWithRed:136/255.0 green:205/255.0 blue:85/255.0 alpha:1];
    
    NSArray * titile = @[@"慢跑",@"中跑",@"快跑",@"健走",@"热身",@"放松",@"街舞音乐"];
    
    cell.titlelabel.text = titile[indexPath.row];
    cell.titlelabel.textColor = [UIColor whiteColor];
    return cell;
}


#pragma mark --- 返回每个item宽和高 ---
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    CGFloat width = (KWIDTH - 40)/3;
    CGFloat height = (KWIDTH - 40)/3 + 35;
    
    return CGSizeMake(width, height);
}
#pragma mark --- 控制集合视图的行边距 ---
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 10;
}

#pragma mark --- 控制集合视图的列边距 ---
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 10;
}







- (void)musiclist
{
    self.musiclistButton.backgroundColor =[UIColor colorWithRed:136/255.0 green:205/255.0 blue:85/255.0 alpha:1];
    self.mymusicButton.backgroundColor = [UIColor lightGrayColor];
    [UIView animateWithDuration:0.5 animations:^{
        [self.scrolllView setContentOffset:CGPointMake(0, 0)];
        
    }];
}

- (void)mymusic
{
    self.mymusicButton.backgroundColor =[UIColor colorWithRed:136/255.0 green:205/255.0 blue:85/255.0 alpha:1];
    self.musiclistButton.backgroundColor = [UIColor lightGrayColor];
    [UIView animateWithDuration:0.5 animations:^{
        [self.scrolllView setContentOffset:CGPointMake(KWIDTH, 0)];
    }];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (scrollView == _scrolllView) {
        if (scrollView.contentOffset.x / KWIDTH == 0) {
            self.musiclistButton.backgroundColor =[UIColor colorWithRed:136/255.0 green:205/255.0 blue:85/255.0 alpha:1];
            self.mymusicButton.backgroundColor = [UIColor lightGrayColor];
        }
        else
        {
            self.mymusicButton.backgroundColor =[UIColor colorWithRed:136/255.0 green:205/255.0 blue:85/255.0 alpha:1];
            self.musiclistButton.backgroundColor = [UIColor lightGrayColor];
            
        }
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
